<template>
<van-button
:icon="value === 1  ? 'good-job' : 'good-job-o' "
:class="{
  liked:value ===1
}"
:loading="loading"
@click="onclick"
>

</van-button>
</template>

<script>

import { addLike ,deleteLike} from '@/api/article'
export default {
  name: "likeArtcle",
  props:{

    value:{
      type:Number,
      required:true
    },
    articlesId:{
      type:[String,Number,Object],
      required: true
    }

  },
  data(){
    return{
      loading:false
    }
  },
  methods:{
   async onclick(){

     this.loading=true

      try {

        let status=-1
        // 如果已经点赞，则取消点赞
        if (this.value === 1) {
          await deleteLike(this.articlesId)

        } else {
          // 否则添加点赞
          await addLike(this.articlesId)
          status=1
        }
        this.$emit('input',status)
        this.$toast.success(status ===1 ?'点赞成功' :'取消点赞')

      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
      this.loading=false

    }
  }
}
</script>

<style scoped lang="less">
.liked{

  .van-icon{
    color: #e5645f;
  }

}
</style>
